package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.ConnectionPoint;
import ru.amse.tsyganov.jumleditor.view.representation.TransitionView;

/*package*/ class MoveTransitionCommand extends Command {
	
	private final TransitionView<? extends Transition> movingTransition;
	
	private final ConnectionPoint oldSourceConnectionPoint;
	
	private final ConnectionPoint oldTargetConnectionPoint;
	
	private final ConnectionPoint newSourceConnectionPoint;
	
	private final ConnectionPoint newTargetConnectionPoint;

	public MoveTransitionCommand(
			TransitionView<? extends Transition> movingTransition, 
			ConnectionPoint newSourceConnectionPoint, 
			ConnectionPoint newTargetConnectionPoint) {
		
		if (movingTransition == null || newSourceConnectionPoint == null ||
				newTargetConnectionPoint == null || 
				newSourceConnectionPoint.getContainer() == 
					newTargetConnectionPoint.getContainer()) {
			
			throw new IllegalArgumentException();
		}
		
		this.movingTransition = movingTransition;
		oldSourceConnectionPoint = movingTransition.getSourceConnectionPoint();
		oldTargetConnectionPoint = movingTransition.getTargetConnectionPoint();
		this.newSourceConnectionPoint = newSourceConnectionPoint;
		this.newTargetConnectionPoint = newTargetConnectionPoint;
		
		if (oldSourceConnectionPoint == newSourceConnectionPoint &&
				oldTargetConnectionPoint == newTargetConnectionPoint) {
			
			throw new IllegalArgumentException();
		}
	}
	
	@Override
	public void execute() {
		movingTransition.getModel().setSource(newSourceConnectionPoint.
				getContainer().getModel().getStateVertex());
		
		movingTransition.getModel().setTarget(newTargetConnectionPoint.
				getContainer().getModel().getStateVertex());
		
		movingTransition.setSourceConnectionPoint(newSourceConnectionPoint);
		movingTransition.setTargetConnectionPoint(newTargetConnectionPoint);
	}

	@Override
	public void unexecute() {
		movingTransition.getModel().setSource(oldSourceConnectionPoint.
				getContainer().getModel().getStateVertex());
		
		movingTransition.getModel().setTarget(oldTargetConnectionPoint.
				getContainer().getModel().getStateVertex());
		
		movingTransition.setSourceConnectionPoint(oldSourceConnectionPoint);
		movingTransition.setTargetConnectionPoint(oldTargetConnectionPoint);
	}

}
